#include "PropSprite.h"
#include <tcLogic/tc_logic.h>
#include <math.h>

void PropSprite::init( tcType propType, tcType owner)
{
	mCurrentPer = 1.0f;
	mPropType = propType;
	mOwner = owner;
	mNeedScore = tc_logic_get_prop_score(propType);
}


bool PropSprite::containsTouchLocation( const CCPoint& location )
{
	return abs(location.x - this->getPosition().x) <= this->getContentSize().width * this->getScale() / 2
		&& abs(location.y - this->getPosition().y) <= this->getContentSize().height * this->getScale() / 2;
}


void PropSprite::drawCDRect(int have)
{		
	int need = this->mNeedScore;
	float per = (float)have / (float)need;
	if(per > 1)
	{
		per = 1;
	}

	glDisable(GL_TEXTURE_2D);
	glDisableClientState(GL_TEXTURE_COORD_ARRAY);
	glDisableClientState(GL_COLOR_ARRAY);
	
	if( per - mCurrentPer < 0.01 && per - mCurrentPer > -0.01 )
	{
		mCurrentPer = per;
	}
	else if( mCurrentPer < per )
	{
        if(mIsFading) {
            mCurrentPer += 0.045f;
        }
        else {
            mCurrentPer += 0.01f;
        }
	}
	else if( mCurrentPer > per )
	{
		if(mIsFading) {
            mCurrentPer -= 0.045f;
        }
        else {
            mCurrentPer -= 0.01f;
        }
	}
	if( mCurrentPer > 1 )
	{
		mCurrentPer = 1;
	}
	if( mCurrentPer <= 0 )
	{
        mIsFading = false;
		mCurrentPer = 0;
	}
	per = mCurrentPer;
	
	
	int shadow_width = 17;
	
	if( per != 1 && mIsFading == false) {
		glColor4f(0.1f, 0.1f, 0.1f, 0.35f);
		CCPoint fullfill[4] = { ccp( this->getPosition().x - shadow_width, this->getPosition().y - shadow_width ), 
			ccp( this->getPosition().x - shadow_width, this->getPosition().y + shadow_width ),
			ccp( this->getPosition().x + shadow_width, this->getPosition().y + shadow_width ),
			ccp( this->getPosition().x + shadow_width, this->getPosition().y - shadow_width )};
  
		glVertexPointer(2, GL_FLOAT, 0, fullfill);
		glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
	}
	
	float radius = shadow_width * 1.414f;
	int points = 0;
	
	CCPoint* vertices = NULL;
	if(mOwner == TC_PLAYER_TWO)
	{
		static CCPoint currentPos = ccp(0, 0);
        if(currentPos.x != this->getPosition().x + radius * sin(2 * M_PI * per) || currentPos.y != this->getPosition().y + radius * cos(2 * M_PI * per)) {
            currentPos = ccp(this->getPosition().x + radius * sin(2 * M_PI * per), this->getPosition().y + radius * cos(2 * M_PI * per) );

            currentPos = ccp(this->getPosition().x + radius * sin(2 * M_PI * per), this->getPosition().y + radius * cos(2 * M_PI * per) );
            if( per < 0.125f )
            {
                temp1[0] = this->getPosition();
                temp1[1] = currentPos;
                temp1[2] = ccp( this->getPosition().x + shadow_width, this->getPosition().y + shadow_width );
                temp1[3] = ccp( this->getPosition().x + shadow_width, this->getPosition().y - shadow_width );
                temp1[4] = ccp( this->getPosition().x, this->getPosition().y - shadow_width );
                vertices = temp1;
                points = 5;
            }
            else if( per < 0.375f )
            {
                temp2[0] = this->getPosition();
                temp2[1] = currentPos;
                temp2[2] = ccp( this->getPosition().x + shadow_width, this->getPosition().y - shadow_width );
                temp2[3] = ccp( this->getPosition().x, this->getPosition().y - shadow_width );
                vertices = temp2;
                points = 4;
            }
            else if( per < 0.5f )
            {
                temp3[0] = this->getPosition();
                temp3[1] = currentPos;
                temp3[2] = ccp( this->getPosition().x, this->getPosition().y - shadow_width );
                vertices = temp3;
                points = 3;
            }
            else if( per < 0.625 )
            {
                
                temp4[0] = ccp( this->getPosition().x, this->getPosition().y + shadow_width );
                temp4[1] = this->getPosition();
                temp4[2] = currentPos;
                temp4[3] = ccp( this->getPosition().x - shadow_width, this->getPosition().y - shadow_width );
                temp4[4] = ccp( this->getPosition().x - shadow_width, this->getPosition().y + shadow_width );
                vertices = temp4;
                points = 5;
            }
            else if( per < 0.875f )
            {
                temp5[0] = ccp( this->getPosition().x, this->getPosition().y + shadow_width );
                temp5[1] = this->getPosition();
                temp5[2] = currentPos;
                temp5[3] = ccp( this->getPosition().x - shadow_width, this->getPosition().y + shadow_width );
                vertices = temp5;
                points = 4;
            }
            else
            {
                temp6[0] = ccp( this->getPosition().x, this->getPosition().y + shadow_width );
                temp6[1] = this->getPosition();
                temp6[2] = currentPos;
                vertices = temp6;
                points = 3;
            }
		
        }
		if( per < 0.5f )
		{
            temp0[0] = ccp( this->getPosition().x, this->getPosition().y + shadow_width );
            temp0[1] = ccp( this->getPosition().x, this->getPosition().y - shadow_width );
            temp0[2] = ccp( this->getPosition().x - shadow_width, this->getPosition().y - shadow_width );
            temp0[3] = ccp( this->getPosition().x - shadow_width, this->getPosition().y + shadow_width );
			glColor4f(0.1f, 0.1f, 0.1f, 0.4f);
			
			glVertexPointer(2, GL_FLOAT, 0, temp0);
			glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
		}
    }
	else
	{
		static CCPoint currentPos = ccp(0 , 0);
        if(currentPos.x != this->getPosition().x + radius * sin(2 * M_PI * per) || currentPos.y != this->getPosition().y + radius * cos(2 * M_PI * per)) {
            currentPos = ccp( this->getPosition().x - radius * sin(2 * M_PI * per), this->getPosition().y - radius * cos(2 * M_PI * per) );
            currentPos = ccp( this->getPosition().x - radius * sin(2 * M_PI * per), this->getPosition().y - radius * cos(2 * M_PI * per) );
            if( per < 0.125f )
            {
                temp1[0] = this->getPosition();
                temp1[1] = currentPos;
                temp1[2] = ccp( this->getPosition().x - shadow_width, this->getPosition().y - shadow_width );
                temp1[3] = ccp( this->getPosition().x - shadow_width, this->getPosition().y + shadow_width );
                temp1[4] = ccp( this->getPosition().x, this->getPosition().y + shadow_width );
                vertices = temp1;
                points = 5;
            }
            else if( per < 0.375f )
            {
                temp2[0] = this->getPosition();
                temp2[1] = currentPos;
                temp2[2] = ccp( this->getPosition().x - shadow_width, this->getPosition().y + shadow_width );
                temp2[3] = ccp( this->getPosition().x, this->getPosition().y + shadow_width );
                vertices = temp2;
                points = 4;
            }
            else if( per < 0.5f )
            {
                temp3[0] = this->getPosition();
                temp3[1] = currentPos;
                temp3[2] = ccp( this->getPosition().x, this->getPosition().y + shadow_width );
                vertices = temp3;
                points = 3;
            }
            else if( per < 0.625 )
            {
                
                temp4[0] = ccp( this->getPosition().x, this->getPosition().y - shadow_width );
                temp4[1] = this->getPosition();
                temp4[2] = currentPos;
                temp4[3] = ccp( this->getPosition().x + shadow_width, this->getPosition().y + shadow_width );
                temp4[4] = ccp( this->getPosition().x + shadow_width, this->getPosition().y - shadow_width );
                vertices = temp4;
                points = 5;
            }
            else if( per < 0.875f )
            {
                temp5[0] = ccp( this->getPosition().x, this->getPosition().y - shadow_width );
                temp5[1] = this->getPosition();
                temp5[2] = currentPos;
                temp5[3] = ccp( this->getPosition().x + shadow_width, this->getPosition().y - shadow_width );
                vertices = temp5;
                points = 4;
            }
            else
            {
                temp6[0] = ccp( this->getPosition().x, this->getPosition().y - shadow_width );
                temp6[1] = this->getPosition();
                temp6[2] = currentPos;
                vertices = temp6;
                points = 3;
            }
        }
		if( per < 0.5f )
		{
            temp0[0] = ccp( this->getPosition().x, this->getPosition().y - shadow_width ); 
            temp0[1] = ccp( this->getPosition().x, this->getPosition().y + shadow_width );
            temp0[2] = ccp( this->getPosition().x + shadow_width, this->getPosition().y + shadow_width);
            temp0[3] = ccp( this->getPosition().x + shadow_width, this->getPosition().y - shadow_width);
			glColor4f(0.1f, 0.1f, 0.1f, 0.4f);
			
			glVertexPointer(2, GL_FLOAT, 0, temp0);
			glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
		}
    }
	
	
	//glLineWidth(0.0f);
	glColor4f(0.1f, 0.1f, 0.1f, 0.4f);
	//glColor4f(1.0, 1.0, 1.0, 0.1);
	
	glVertexPointer(2, GL_FLOAT, 0, vertices);
	glDrawArrays(GL_TRIANGLE_FAN, 0, points);
	
	// restore default state
	glEnableClientState(GL_COLOR_ARRAY);
	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
	glEnable(GL_TEXTURE_2D);
}

tcType PropSprite::getPropType()
{
	return mPropType;
}

tcType PropSprite::getOwner()
{
	return mOwner;
}
